package cn.chinaflame.pagodas.utils.stl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Static functions to simplifiy common {@link java.security.MessageDigest} tasks.  This
 * class is thread safe.
 * 
 * @author pengjiong
 *
 */
public class MD5Util {

    private MD5Util() {
    }

    /**
     * Returns a MessageDigest for the given <code>algorithm</code>.
     * 
     * @param algorithm
     *            The MessageDigest algorithm name.
     * @return An MD5 digest instance.
     * @throws RuntimeException
     *             when a {@link java.security.NoSuchAlgorithmException} is
     *             caught
     */

    static MessageDigest getDigest() {
        try {
            return MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Calculates the MD5 digest and returns the value as a 16 element
     * <code>byte[]</code>.
     * 
     * @param data
     *            Data to digest
     * @return MD5 digest
     */
    public static byte[] md5(byte[] data) {
        return getDigest().digest(data);
    }

    /**
     * Calculates the MD5 digest and returns the value as a 16 element
     * <code>byte[]</code>.
     * 
     * @param data
     *            Data to digest
     * @return MD5 digest
     */
    public static byte[] md5(String data) {
        return md5(data.getBytes());
    }

    /**
     * Calculates the MD5 digest and returns the value as a 32 character hex
     * string.
     * 
     * @param data
     *            Data to digest
     * @return MD5 digest as a hex string
     */
    public static String md5Hex(byte[] data) {
        return HexUtil.toHexString(md5(data));
    }

    /**
     * Calculates the MD5 digest and returns the value as a 32 character hex
     * string.
     * 
     * @param data
     *            Data to digest
     * @return MD5 digest as a hex string
     */
    public static String md5Hex(String data) {
        return HexUtil.toHexString(md5(data));
    }
    
    /**
     * 验证输入的数据src加密后是否与dest相等.
     * @param src 输入的数据
     * @param dest 加密后的数据
     * @return
     */
    public static boolean validateData(String src,String dest){
    	src = StringUtil.null2String(src);
    	dest = StringUtil.null2String(dest);
    	MD5 md5 = new MD5();
    	if(("".equals(src) && src.equals(dest)) || md5Hex(src).equals(dest)){
    		return true;
    	}
    	if(md5.getMD5ofStr(src).equals(dest)){
    		return true;
    	}
    	return false;
    }
    
    public static void main(String[]args){
    	
    	String key = "30819f300d06092a864886f70d010101050003818d0030818902818100b3192e075fad9b18848a929e8fe523729915b3bbd714d1fbe3713bf0139e951647dd2c2d62a26e54e3b2ddb55c97be2b332dc12c4fc8d67c4918055f87f29eeca55d131c0dcf2d918157ebc0bbe23f073de0d871176a169e963b100773b61e6f4d12efd3be185f4a3ca7d73d51a490e90a570913040ef3e65116bd796a5e39d30203010001";
    	String source = "{\"head\":{\"serCode\":\"203005\",\"merCode\":\"10000000006\",\"orderId\":\"QA1523867877553\",\"merDate\":\"20180416\",\"merTime\":\"163757\",\"versionNo\":\"1.0.0\",\"signType\":\"2\"},\"content\":{\"payeeName\":\"赵红佳\",\"payeeAccount\":\"6217002670013083078\",\"bankName\":\"中国建设银行\",\"orgCode\":\"ccb\",\"orderAmount\":\"1000\",\"channelCode\":\"PAYWORTH\",\"noticeUrl\":\"http://101.132.71.10/webgate/pay.htm\"}}";
    	
    	System.out.println("192.168.1.219");
    	String md5Str = md5Hex(source);
    	System.out.println(md5Str);
    	
    	String md5StrKey = md5Str+key;
    	System.out.println(md5Hex(md5StrKey));
    }
}
